/**
 * @description:
 * @author: 梅雄
 * @createDate: 2021/12/26
 * @version: 1.0
 * @status: done
 */
package com.xhwl.centiotdata.suppliersystem.eesd.nantouC;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.xhwl.centiotdata.pojo.dto.eesd.EesdConfigDTO;
import com.xhwl.centiotdata.pojo.enums.eesd.EesdMethodEnum;
import com.xhwl.centiotdata.service.integrateconfig.EESDIntegrationConfig;
import com.xhwl.centiotdata.utils.RedisUtils;
import com.xhwl.common.utils.ChkUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 望美制造 - 资产管理
 * mx
 */
@Component
@Slf4j
public class C2HandlerUtil {

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    private EESDIntegrationConfig eesdIntegrationConfig;

    /**
     * 获取C2配置
     */
    private EesdConfigDTO.C2 getC2Config() {
        List<EesdConfigDTO> integrationConfig = eesdIntegrationConfig.getIntegrationConfig(EesdMethodEnum.C2.getId());
        if (ChkUtil.isNull(integrationConfig)) {
            throw new RuntimeException("配置无法获取");
        }
        EesdConfigDTO eesdConfigDTO = integrationConfig.get(0);
        return eesdConfigDTO.getC2();
    }

    /**
     * 1.获取token
     */
    private String getToken(EesdConfigDTO.C2 c2) {
        Object ret = redisUtils.get("C2.TOKEN");
        if (ret != null) {
            return (String) ret;
        }
        String host = c2.getAuthHost() + "/admin/client/getToken";
        log.info("请求token-c2:{}", c2);
        log.info("请求token-url:{}", host);
        Map<String, Object> map = Maps.newHashMap();
        map.put("appKey", c2.getAppKey());
        map.put("appSecret", c2.getAppSecret());
        //发起鉴权
        String result = HttpUtil.get(host, map, 3000);
        if (result == null) {
            throw new RuntimeException("result是null");
        }
        Map<String, Object> res = JSON.parseObject(result, Map.class);
        if (res == null) {
            throw new RuntimeException(result);
        }
        if (ChkUtil.getInteger(res.get("code")) != 200) {
            throw new RuntimeException(result);
        }
        String accessToken = (String) res.get("data");
        redisUtils.setWithMinute("C2.TOKEN", accessToken, 24 * 60 - 1);
        return accessToken;
    }

    /**
     * 统一处理结果集
     */
    private Object checkRes(String result) {
        if (ChkUtil.isNull(result)) {
            throw new RuntimeException(result);
        }
        Map map = JSON.parseObject(result, Map.class);
        if (ChkUtil.isNull(map)) {
            throw new RuntimeException(result);
        }
        if (!"0".equals(ChkUtil.getString(map.get("code")))) {
            throw new RuntimeException(result);
        }
        return map.get("data");
    }

    /**
     * 2.查询月碳排放量数据
     */
    Object getMonthC2() {
        EesdConfigDTO.C2 c2 = getC2Config();
        String token = getToken(c2);
        String host = c2.getApiHost() + "/ioc/carbon/overview/emission/last_month";
        String result = HttpRequest.get(host).header("Authorization", token).execute().body();
        return checkRes(result);
    }

    /**
     * 3.查询年碳排放量数据
     */
    Object getYearC2() {
        EesdConfigDTO.C2 c2 = getC2Config();
        String token = getToken(c2);
        String host = c2.getApiHost() + "/ioc/carbon/overview/emission/year";
        String result = HttpRequest.get(host).header("Authorization", token).execute().body();
        return checkRes(result);
    }

    /**
     * 4.查询碳排放曲线数据
     */
    Object getC2Discharge(String curveType) {
        EesdConfigDTO.C2 c2 = getC2Config();
        String token = getToken(c2);
        String host = c2.getApiHost() + "/ioc/carbon/emissions/curve";
        HashMap<String, Object> objectObjectHashMap = Maps.newHashMap();
        objectObjectHashMap.put("curveType", curveType);
        String result = HttpRequest.post(host).body(JSON.toJSONString(objectObjectHashMap), "application/json").header("Authorization", token).execute().body();
        return checkRes(result);
    }

    /**
     * 5.查询碳排放构成数据
     */
    Object getC2Build(Integer formationType) {
        EesdConfigDTO.C2 c2 = getC2Config();
        String token = getToken(c2);
        String host = c2.getApiHost() + "/ioc/carbon/emissions/formation";
        HashMap<String, Object> objectObjectHashMap = Maps.newHashMap();
        objectObjectHashMap.put("formationType", formationType);
        String result = HttpRequest.post(host).body(JSON.toJSONString(objectObjectHashMap), "application/json").header("Authorization", token).execute().body();
        return checkRes(result);
    }

    /**
     * 6.查询降碳效果数据
     */
    Object getC2Reduce(Integer reductionType) {
        EesdConfigDTO.C2 c2 = getC2Config();
        String token = getToken(c2);
        String host = c2.getApiHost() + "/ioc/carbon/emissions/reduction_effect";
        HashMap<String, Object> objectObjectHashMap = Maps.newHashMap();
        objectObjectHashMap.put("reductionType", reductionType);
        String result = HttpRequest.post(host).body(JSON.toJSONString(objectObjectHashMap), "application/json").header("Authorization", token).execute().body();
        return checkRes(result);
    }

    /**
     * 7.查询碳排放指数数据
     */
    Object getC2Index(Integer exponentType) {
        EesdConfigDTO.C2 c2 = getC2Config();
        String token = getToken(c2);
        String host = c2.getApiHost() + "/ioc/carbon/emissions/exponent";
        HashMap<String, Object> objectObjectHashMap = Maps.newHashMap();
        objectObjectHashMap.put("exponentType", exponentType);
        String result = HttpRequest.post(host).body(JSON.toJSONString(objectObjectHashMap), "application/json").header("Authorization", token).execute().body();
        return checkRes(result);
    }

    /**
     * 8.查询优化建议数据
     */
    Object getC2Suggestion() {
        EesdConfigDTO.C2 c2 = getC2Config();
        String token = getToken(c2);
        String host = c2.getApiHost() + "/ioc/carbon/emissions/suggestion";
        String result = HttpRequest.post(host).header("Authorization", token).execute().body();
        return checkRes(result);
    }
}
